//
//  LoginViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/19.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "LoginViewController.h"
#import "WebViewController.h"

@interface LoginViewController ()

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UITextField *phoneTF;
@property (nonatomic, strong)UITextField *pwTF;
@property (nonatomic, strong)UIButton *rememberBtn;
@property (nonatomic, strong)UIButton *loginBtn;
@property (strong, nonatomic) UIButton *protocolBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"登录";
    [self customBackButton];
    [self buildUI];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.protocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginBtn.mas_bottom).offset(10);
        make.left.equalTo(self.loginBtn.mas_left);
        make.height.equalTo(@30);
    }];
}

- (void)buildUI {
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.phoneTF];
    [self.view addSubview:self.pwTF];
    [self.view addSubview:self.rememberBtn];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.protocolBtn];
}

#pragma mark - event repponse
- (void)rememberBtnClickAction {
    _rememberBtn.selected = !_rememberBtn.selected;
}

- (void)loginBtnClickAction {
    if (self.phoneTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入用户名"];
    }else if (self.pwTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入密码"];
    }else {
        [CXDNetHelp getRequestWithURLString:LoginURL parameters:@{@"username":_phoneTF.text,@"password":_pwTF.text} success:^(id  _Nullable responseObject) {
            if ([[responseObject objectForKey:@"zhuangtai"] isEqualToString:@"1"]) {
                [CXDHelper rememberPasswordWithResult:_rememberBtn.selected];
                [CXDHelper userLoginStatusWithBool:YES];
                [CXDHelper saveUserAccountWithAccountName:self.phoneTF.text];
                [CXDNoticeView showNoticeViewWithString:[responseObject objectForKey:@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [CXDNoticeView showNoticeViewWithString:[responseObject objectForKey:@"message"]];
            }
        } failure:^(NSError * _Nullable error) {
            
        }];
    }
}

- (void)protocolBtnDidClick:(UIButton *)sender {
    
    WebViewController *vc = [[WebViewController alloc] init];
    vc.titleString = @"隐私政策";
    vc.url = @"http://m.wenqujingdian.com/pages/privacy/";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, kScreenWidth - 20, 20)];
        _titleLabel.text = @"用户名登录";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = MainDarkGrayColor;
    }
    return _titleLabel;
}

- (UITextField *)phoneTF {
    if (!_phoneTF) {
        _phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(30, self.titleLabel.bottom + 40, kScreenWidth - 60, 40)];
        _phoneTF.placeholder = @"请输入用户名";
        _phoneTF.layer.borderColor = [UIColor colorFromHexCode:@"#eeeeee"].CGColor;
        _phoneTF.layer.borderWidth = 0.8f;
        _phoneTF.layer.cornerRadius = 8.f;
        _phoneTF.layer.masksToBounds = YES;
        _phoneTF.tintColor = MainGreenColor;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
        [imageView setImage:[UIImage imageNamed:@"phoneIcon"]];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        _phoneTF.leftView = imageView;
        _phoneTF.leftViewMode = UITextFieldViewModeAlways;
    }
    return _phoneTF;
}

- (UITextField *)pwTF {
    if (!_pwTF) {
        _pwTF = [[UITextField alloc] initWithFrame:CGRectMake(30, self.phoneTF.bottom + 15, kScreenWidth - 60, 40)];
        _pwTF.placeholder = @"请输入登录密码";
        _pwTF.layer.borderColor = [UIColor colorFromHexCode:@"#eeeeee"].CGColor;
        _pwTF.layer.borderWidth = 0.8f;
        _pwTF.layer.cornerRadius = 8.f;
        _pwTF.layer.masksToBounds = YES;
        _pwTF.tintColor = MainGreenColor;
        _pwTF.secureTextEntry = YES;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
        [imageView setImage:[UIImage imageNamed:@"pwIcon"]];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        _pwTF.leftView = imageView;
        _pwTF.leftViewMode = UITextFieldViewModeAlways;
    }
    return _pwTF;
}

- (UIButton *)rememberBtn {
    if (!_rememberBtn) {
        _rememberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rememberBtn.frame = CGRectMake(30, self.pwTF.bottom + 10, 80, 30);
        [_rememberBtn setImage:[UIImage imageNamed:@"rectangle"] forState:UIControlStateNormal];
        [_rememberBtn setImage:[UIImage imageNamed:@"remember_account"] forState:UIControlStateSelected];
        _rememberBtn.selected = YES;
        _rememberBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [_rememberBtn setTitle:@"记住密码" forState:UIControlStateNormal];
        _rememberBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_rememberBtn setTitleColor:RGB(109, 109, 109) forState:UIControlStateNormal];
        [_rememberBtn addTarget:self action:@selector(rememberBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rememberBtn;
}

- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(30, self.rememberBtn.bottom + 20, kScreenWidth - 60, 40);
        _loginBtn.backgroundColor = MainGreenColor;
        [_loginBtn setTitle:@"确认登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        _loginBtn.layer.cornerRadius = 8.f;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn addTarget:self action:@selector(loginBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}
- (void)customBackButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
    
    backBtn.frame = CGRectMake(0, 0, 60, 40);
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = item;
}

- (UIButton *)protocolBtn {
    
    if (_protocolBtn == nil) {
        _protocolBtn = [[UIButton alloc] init];
        _protocolBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_protocolBtn setTitle:@"《隐私协议》" forState:UIControlStateNormal];
        [_protocolBtn setTitleColor:MainGreenColor forState:UIControlStateNormal];
        [_protocolBtn addTarget:self action:@selector(protocolBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _protocolBtn;
}

// 返回按钮按下
- (void)backBtnClicked:(UIButton *)sender{
    if (self.backHome == YES) {
        [self.tabBarController setSelectedIndex:1];
    }
    // pop
    [self.navigationController popViewControllerAnimated:YES];
}
@end
